<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>人员同步</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<style>
    .layui-form-pane .layui-form-label {
        width: auto !important;
    }
</style>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form " action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: auto">门禁号</label>
                            <div class="layui-input-inline" style="width: 130px">
                                <select name="sex" lay-verify="required">
                                    <option value=""></option>
                                    <option value="0">所有</option>
                                    <option value="1">部分</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="checkbox" name="system[2]" lay-skin="primary" title="只显示未同步的名单">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 查询
                            </button>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn "> 更新至设备
                            </button>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn "> 完全更新(包含失败)
                            </button>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"> 同步至设备
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <fieldset class="table-search-fieldset">
            <legend>访客名单</legend>
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </fieldset>

    </div>
    <script type="text/html" id="currentTableBar">
        {{# if(d.remark === "已上传"){ }}
        <a class="layui-btn layui-btn-xs layui-btn-disabled data-count-edit" lay-event="edit">上传信息</a>
        {{# }else if(d.remark === "未上传"){ }}
        <a class="layui-btn layui-btn-xs  data-count-edit" lay-event="edit">上传信息</a>
        {{# } }}
    </script>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'tree', 'util', 'layer','laytpl'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        let upData = null;

        table.render({
            elem: '#currentTableId',
            url: '/getPerson',
            toolbar: '#toolbarDemo',
            width: 1250,
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData:function(msg){
                upData = msg.data;
                console.log(msg);
                for (let i = 0; i < msg.data.length; i++) {
                    msg.data[i].gender=`${msg.data[i].gender}`.replace("0","男").replace("1","女");
                    msg.data[i].cardMode=`${msg.data[i].cardMode}`.replace("0","教师").replace("1","学生");
                    msg.data[i].personType=`${msg.data[i].personType}`.replace("0","白名单").replace("1","黑名单");
                    msg.data[i].remark=`${msg.data[i].remark}`.replace("0","已上传").replace("1","未上传");
                }
                return msg;
            },
            cols: [ [
                {field: 'id', width: 80, title: '序号'},
                {field: 'name', width: 100, title: '姓名'},
                {field: 'gender', width: 80, title: '性别'},
                {field: 'cardMode', width: 80, title: '职务'},
                {field: 'personType', width: 120, title: '名单类型'},
                {field: 'idCard', width: 180, title: '证件号'},
                {field: 'address', width: 180, title: '地址'},
                {field: 'deviceID', width: 100, title: '设备ID'},
                {field: 'remark', width: 80, title: '状态'},
                {title: '操作', minWidth: 80, toolbar: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });



        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/perManage/addAndEdit',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                let data = upData[obj.data.id-1];
                $.ajax({
                    url:`/addPersonUp`,
                    type:`post`,
                    dataType:`json`,
                    data: data,
                    timeout:10000,
                    success:function (res) {
                        console.log(res);
                        if (res.code == 200){
                            layer.msg('添加成功');
                            layer.close(layer.index);
                            window.parent.location.reload();
                        }else{
                            layer.msg('添加失败');
                        }
                    }
                });
                return false;
            }
        });
    });
</script>
</body>
</html>